package org.apache.servicecomb.samples.practise.houserush.user.center.api;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.apache.servicecomb.samples.practise.houserush.user.center.aggregate.Building;
import org.apache.servicecomb.samples.practise.houserush.user.center.aggregate.House;
import org.apache.servicecomb.samples.practise.houserush.user.center.aggregate.Realestate;
import org.apache.servicecomb.samples.practise.houserush.user.center.service.RealestateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: Snake Man
 * @system: 控制层
 * @DateTime: 2021/12/26 21:31
 * @Description:
 */
@RestSchema(schemaId = "realestateApiRest")
//声明使用springMVC
@RequestMapping("/")
public class RealestateApiImpl implements RealestateApi{

    @GetMapping("/testProvider")
    public Date testProvider() {
        return new Date();
    }

    @Autowired
    private RealestateService realestateService;

    /**
     * 新增楼盘
     * @param realestate
     * @return
     */
    @PostMapping("/realestates")
    public Realestate createRealestate(@RequestBody Realestate realestate) {
        return realestateService.createRealesate(realestate);
    }

    /**
     * 查询楼盘
     * @param id
     * @return
     */
    @GetMapping("/realestates/{id}")
    public Realestate findRealestate(@PathVariable("id") int id) {
        return realestateService.findRealestate(id);
    }

    /**
     * 查询所有楼盘
     * @return
     */
    @GetMapping("realestates")
    public List<Realestate> indexRealestates() {
        return realestateService.indexRealestates();
    }

    /**
     * 修改楼盘
     * @param id
     * @param realestate
     * @return
     */
    @PutMapping("realestates/{id}")
    public Realestate updateRealestate(@PathVariable("id") int id,@RequestBody Realestate realestate) {
        realestate.setId(id);
        return realestateService.updateRealestate(realestate);
    }

    /**
     * 删除楼盘
     * @param id
     */
    @DeleteMapping("realestates/{id}")
    public void removeRealestate(@PathVariable("id")int id) {
        realestateService.removeRealestate(id);
    }


    /**
     * 新增建筑楼
     * @param realestateId 楼盘id
     * @param building 楼栋信息
     * @return
     */
    @PostMapping("realestates/{realestateId}/buildings")
    public Building createBuilding(@PathVariable("realestateId") int realestateId,
                                   @RequestBody Building building) {
        return realestateService.createBuilding(realestateId, building);
    }


    /**
     * 查询建筑楼信息
     * @param id
     * @return
     */
    @GetMapping("buildings/{id}")
    public Building findBuilding(@PathVariable("id") int id) {
        return realestateService.findBuilding(id);
    }


    /**
     * 修改建筑楼信息
     * @param id
     * @param building
     * @return
     */
    @PutMapping("buildings/{id}")
    public Building updateBuilding(@PathVariable("id") int id,
                                   @RequestBody Building building) {
        building.setId(id);
        return realestateService.updateBuilding(building);
    }


    /**
     * 删除建筑楼
     * @param id
     */
    @DeleteMapping("buildings/{id}")
    public void removeBuilding(@PathVariable("id") int id) {
        realestateService.removeBuilding(id);
    }

    /**
     * 查询楼盘里的所有建筑楼信息
     * @param
     * @param
     * @return
     */
    @GetMapping("realestates/{realestateId}/buildings")
    public List<Building> indexBuildings(@PathVariable("realestateId") int realestateId) {
        return realestateService.indexBuildings(realestateId);
    }

    /**
     * 新增房源
     * @param buildingId
     * @param house
     * @return
     */
    @PostMapping("buildings/{buildingId}/houses")
    public House createHouse(@PathVariable("buildingId") int
                                     buildingId, @RequestBody House house) {
        return realestateService.createHouse(buildingId, house);
    }

    /**
     * 查询房源信息
     * @param id
     * @return
     */
    @GetMapping("houses/{id}")
    public House findHouse(@PathVariable("id") int id) {
        return realestateService.findHouse(id);
    }

    /**
     * 查询建筑类下的所有房源信息
     * @param buildingId
     * @return
     */
    @GetMapping("buildings/{buildingId}/houses")
    public List<House> indexHouses(@PathVariable("buildingId")
                                           int buildingId) {
        return realestateService.indexHouses(buildingId);
    }

    /**
     * 修改房源
     * @param id
     * @param house
     * @return
     */
    @PutMapping("houses/{id}")
    public House updateHouse(@PathVariable("id") int id,
                             @RequestBody House house) {
        house.setId(id);
        return realestateService.updateHouse(house);
    }

    /**
     * 删除房源
     * @param id
     */
    @DeleteMapping("houses/{id}")
    public void removeHouse(@PathVariable("id") int id) {
        realestateService.removeHouse(id);
    }

    /**
     * 根据楼盘查询房源，显示未锁定房源
     * @param id
     * @return
     */
    @GetMapping("/realestates/findTreeRealestate/{id}")
    public Realestate findByRealestateId(@PathVariable("id") Integer id) {
        //根据id查询楼盘
        Realestate realestate = realestateService.findRealestate(id);
        //遍历楼栋信息
        realestate.getBuildings().forEach(building -> {
            //获取楼栋的房源信息
            List<House> houses = building.getHouses();
            Iterator<House> it = houses.iterator();
            //遍历房源信息
            while (it.hasNext()){
                House house = (House) it.next();
                //判断房源的状态是否locking
                if("locking".equals(house.getState())){
                    //移除锁定的房源
                    it.remove();
                }
            }
        });
        return realestate;
    }

    /**
     * 锁定房源
     * @param ids 房源id集合
     * @return
     */
    @PutMapping("houses/lock_houses_for_sale")
    public List<House> lockHousesForSale(@RequestBody List<Integer> ids) {
        return realestateService.lockHousesForSale(ids);
    }

    /**
     * 解锁房源
     * @param ids
     * @return
     */
    @PutMapping("houses/releaseLockingStatesForHouses")
    public int updateReleaseLockingStattesForHouses(@RequestBody List<Integer> ids) {
        return realestateService.updateReleaseLockingStattesForHouses(ids);
    }
}
